package com.hdoit.apollo.logical.shop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.common.enums.billing.PayState;
import com.hdoit.apollo.logical.common.enums.shop.CustomerRecordType;
import com.hdoit.apollo.logical.common.enums.shop.PaymentState;
import com.hdoit.apollo.logical.shop.dao.IPaymentDao;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.Payment;
import com.hdoit.apollo.logical.shop.service.ICustomerRecordService;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.shop.service.IPaymentService;
import com.hdoit.apollo.logical.viewmodel.shop.PaymentSearch;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;

import cn.hutool.core.convert.Convert;
/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年05月04日 15:06:20
 * @comment
 */
@Component
public class PaymentServiceImpl extends SuperService<Payment> implements IPaymentService{
	
	@Autowired
	private ICustomerService customerService;
	
	@Autowired
	private ICustomerRecordService customerRecordService;
	
	@Autowired
	private IPaymentDao paymentDao;
	
	@Autowired
	private IBillingService billingService;
	
	@Override
	public boolean applyPayment(List<Payment> payments,String customerId) {
		Customer customer = customerService.queryModel(customerId);
		for(Payment payment:payments) {
			if(PaymentState.pay.equals(payment.getPaymentState())) {
				
				customerRecordService.saveCustomerRecord(payment.getCustomerId(),0.0,payment.getAmount(),CustomerRecordType.pay,
						payment.getPaymentId(),payment.getShopId(),payment.getCreateUserId(),payment.getCreateUserName());
				customer.setPaymentMoney(Convert.toDouble(customer.getPaymentMoney(), 0.0)+payment.getAmount());
				customer.setCustomerDebt(Convert.toDouble(customer.getCustomerDebt(), 0.0)-payment.getAmount());
			}else if(PaymentState.refund.equals(payment.getPaymentState())) {
				customerRecordService.saveCustomerRecord(payment.getCustomerId(),0.0,payment.getAmount(),CustomerRecordType.refund,
						payment.getPaymentId(),payment.getShopId(),payment.getCreateUserId(),payment.getCreateUserName());
				customer.setPaymentMoney(Convert.toDouble(customer.getPaymentMoney(),0.0)-payment.getAmount());
				customer.setCustomerDebt(Convert.toDouble(customer.getCustomerDebt(),0.0)+payment.getAmount());
			}
			customer.setModelStatus(ModelStatus.update);
		}
		if(super.applyChanges(payments)){
			return customerService.applyChange(customer);
		}
		return false;
	}

	@Override
	public Payment queryModelById(String paymentId) {
		return paymentDao.queryModelById(paymentId);
	}

	@Override
	public boolean applyPaymentAndBilling(Payment payment) {
		if(super.applyChange(payment)) {
			Billing billing =billingService.getBillingByKey(payment.getBillingId());
			billing.setModelStatus(ModelStatus.update);
			billing.setPaidAmount(Convert.toDouble(billing.getPaidAmount(),0.0)+payment.getAmount());
			
			billing.setUnpaidAmount(Convert.toDouble(billing.getUnpaidAmount(),0.0)-payment.getAmount());
			if(billing.getUnpaidAmount()<=0){
				billing.setPayState(PayState.paid);
			}
			if(billing.getUnpaidAmount()>0){
				billing.setPayState(PayState.portion);
			}
			
			Customer customer=customerService.queryModel(payment.getCustomerId());
			customer.setModelStatus(ModelStatus.update);
			if(PaymentState.pay.equals(payment.getPaymentState())) {
				customerRecordService.saveCustomerRecord(payment.getCustomerId(),0.0,payment.getAmount(),CustomerRecordType.pay,
						payment.getPaymentId(),payment.getShopId(),payment.getCreateUserId(),payment.getCreateUserName());
				customer.setPaymentMoney(Convert.toDouble(customer.getPaymentMoney(),0.0)+payment.getAmount());
				customer.setCustomerDebt(Convert.toDouble(customer.getCustomerDebt(),0.0)-payment.getAmount());
			}else if(PaymentState.refund.equals(payment.getPaymentState())) {
				customerRecordService.saveCustomerRecord(payment.getCustomerId(),0.0,payment.getAmount(),CustomerRecordType.refund,
						payment.getPaymentId(),payment.getShopId(),payment.getCreateUserId(),payment.getCreateUserName());
				customer.setPaymentMoney(Convert.toDouble(customer.getPaymentMoney(),0.0)-payment.getAmount());
				customer.setCustomerDebt(Convert.toDouble(customer.getCustomerDebt(),0.0)+payment.getAmount());
			}
			
			if(billingService.applyChange(billing)) {
				return customerService.applyChange(customer);
			}
		}
		return false;
	}

	

	@Override
	public List<Payment> queryListMPForPurchase(PaymentSearch search,
			PagerModel pagerModel) {
		
		return paymentDao.queryListMPForPurchase(search,pagerModel);
	}

	@Override
	public int queryCountMPForPurchase(PaymentSearch search) {
		
		return paymentDao.queryCountMPForPurchase(search);
	}

	@Override
	public List<Payment> queryAccountDetail(PaymentSearch search) {
		
		return paymentDao.queryAccountDetail(search);
	}

}